<template>
  <div class="chain">
    <div class="message">以下用户信息储存在区块链中，确保去中心化，不可篡改，保护用户信息安全。</div>
    <a-table
        :columns="columns"
        :pagination="false"
        :data-source="dataSource"
        bordered
        :tableLayout="`fixed`"
        :loading="tableLoading"
        :defaultExpandAllRows="true"
      >
      <template slot="avatarUrl" slot-scope="text, record">
          <span>
            <img
              :src="record.avatarUrl"
              style="height: 40px; width: 40px; border-radius: 10px"
            />
          </span>
      </template>
      
      </a-table>
  </div>
</template>

<script>
import { getTotalSales, getQuantitySold, getOrderNum } from "@/api/report.js";
import Web3 from "web3"
import contract from '../../contract.json'
export default {
  methods: {
    //初始化web3实例
    async initWeb3() {
      const geerliWS ="http://119.29.149.223:8545";
      this.web3 = new Web3(Web3.givenProvider || geerliWS);
      // 获取metamask钱包使用的用户
      this.accounts = await this.web3.eth.requestAccounts();
      console.log( this.accounts)
      this.initContract();
    },
    // 初始化智能合约
    initContract() {
      // 智能合约地址
      this.HccCont = new this.web3.eth.Contract(
        contract.abi,
        "0x7ABb67F5488481D68b33d5701657C7aA32674C67"
      );
      this.getInfo();
      // this.send()
    },
 
    async getInfo() {
      try {
          let re=await this.HccCont.methods.getValuesWithPrefix("mis:block:userinfo").call()

          for(let i=1;i++;i<100000){
            if(re[i]===undefined){
              console.log("停了")
              this.tableLoading=false
              break;
            }
            let customer=JSON.parse(re[i])
            customer.key=i
            this.dataSource.push(customer)
          }
      } catch (error) {
        console.log(error)
        
      }
    },

    // send() {
    //   this.HccCont.methods.setValue(2,JSON.stringify({
    //         key: '5',
    //         nickName: '布吉倒',
    //         avatarUrl: "http://dummyimage.com/100x100",
    //         createTime:"2022-8-9",
    //         updateTime:"2023-2-19"
    //       })).send({
    //     from: this.accounts[0],
    //   })
    //   .on("receipt",function(receipt){
    //     console.log("交易成功")
    //   })
    // },
    
  },
  created() {
    this.initWeb3();
  },
  data(){
    return{
      tableLoading:true,
      dataSource: [
          
        ],

        columns: [
          {
            title: '昵称',
            dataIndex: 'nickName',
            align: "center",
            key: 'nickName',
          },
          {
            title: '头像',
            dataIndex: 'avatarUrl',
            align: "center",
            key: 'avatarUrl',
            scopedSlots: { customRender: "avatarUrl" },
          },
          {
            title: '首次光顾时间',
            dataIndex: 'createTime',
            align: "center",
            key: 'createTime',
          },
          {
            title: '最后光顾时间',
            dataIndex: 'updateTime',
            key: 'updateTime',
            align: "center",
          }
        ]
    }
  },
};
</script>

<style scoped>
.chain{
  padding:30px;

}
.message{
    padding: 11px 38px;
    margin: 0px auto 20px;
    text-align: center;
    font-size: 15px;
    color: rgb(240, 242, 245);
    background-color: rgb(44, 44, 44);
    border-radius: 15px;
    display: inline-block;

}
</style>
